package com.cm386.final_project_timezonealert.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class TimeZoneService {
	public static String toTimeZoneString(TimeZone timeZone){
		SimpleDateFormat sdf  = new SimpleDateFormat("'GMT' Z");
		sdf.setTimeZone(timeZone);
		Calendar calendar	  = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
		Date currentLocalTime = calendar.getTime();
		String timeZoneOffsetString = sdf.format(currentLocalTime);
		return (new StringBuilder(timeZoneOffsetString)).insert(timeZoneOffsetString.length() - 2, ":").toString();
	}
	
	public static String toCurrentHourString(TimeZone timeZone){
		SimpleDateFormat sdf  = new SimpleDateFormat("HH");
		sdf.setTimeZone(timeZone);
		Calendar calendar	  = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
		Date currentLocalTime = calendar.getTime();
		return sdf.format(currentLocalTime);
	}
	
	public static String toCurrentMinutesString(TimeZone timeZone){
		SimpleDateFormat sdf  = new SimpleDateFormat("mm");
		sdf.setTimeZone(timeZone);
		Calendar calendar	  = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
		Date currentLocalTime = calendar.getTime();
		return sdf.format(currentLocalTime);
	}

	public static String toDestinationHourStringByReference(int sourceHour, int sourceMinute,
			TimeZone fromTimeZone, TimeZone toTimeZone) {
		Calendar  calendar1 = Calendar.getInstance();
//		calendar1.set(Calendar.YEAR, 2013);
//		calendar1.set(Calendar.MONTH, 8);
//		calendar1.set(Calendar.DAY_OF_YEAR, 21);
		calendar1.set(Calendar.HOUR_OF_DAY, sourceHour);
		calendar1.set(Calendar.MINUTE, sourceMinute);

        calendar1.setTimeZone(fromTimeZone);
        calendar1.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);
        if (fromTimeZone.inDaylightTime(calendar1.getTime())) {
            calendar1.add(Calendar.MILLISECOND, calendar1.getTimeZone().getDSTSavings() * -1);
        }

        calendar1.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());
        if (toTimeZone.inDaylightTime(calendar1.getTime())) {
            calendar1.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
        }
        SimpleDateFormat formater = new SimpleDateFormat("HH");
        formater.setTimeZone(fromTimeZone);
        return formater.format(calendar1.getTime());
//		return String.valueOf(calendar1.get(Calendar.HOUR_OF_DAY));
	}
	
	public static String toDestinationMinutesStringByReference(int sourceHour, int sourceMinute,
			TimeZone fromTimeZone, TimeZone toTimeZone) {
		Calendar  calendar1 = Calendar.getInstance();
//		calendar1.set(Calendar.YEAR, 2013);
//		calendar1.set(Calendar.MONTH, 8);
//		calendar1.set(Calendar.DAY_OF_YEAR, 21);
		calendar1.set(Calendar.HOUR_OF_DAY, sourceHour);
		calendar1.set(Calendar.MINUTE, sourceMinute);

        calendar1.setTimeZone(fromTimeZone);
        calendar1.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);
        if (fromTimeZone.inDaylightTime(calendar1.getTime())) {
            calendar1.add(Calendar.MILLISECOND, calendar1.getTimeZone().getDSTSavings() * -1);
        }

        calendar1.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());
        if (toTimeZone.inDaylightTime(calendar1.getTime())) {
            calendar1.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
        }
        SimpleDateFormat formater = new SimpleDateFormat("mm");
        formater.setTimeZone(fromTimeZone);
        return formater.format(calendar1.getTime());
		//return String.valueOf(calendar1.get(Calendar.MINUTE));
	}
}
